package com.chzsyh.evwer;

import android.util.Log;

import androidx.annotation.Nullable;

import org.jetbrains.annotations.NotNull;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;

/**
 * 创建日期 2022/6/16
 * 描述：
 */
public class WSManager {

    private static String TAG = "WSManager";

    String mWbSocketUrl;
    OkHttpClient mClient;
    WebSocket mWebSocket;

    public static WSManager getInstance(){
        return new WSManager();
    }

    public void init(String mwbsocketurl) {

//        mwbsocketurl = "ws://jimu.test/websocket";

        mClient = new OkHttpClient.Builder()

                .pingInterval(5, TimeUnit.SECONDS)

                .build();

        Request request = new Request.Builder()
                .url(mwbsocketurl)
//                .addHeader("clientId","and101607"+System.currentTimeMillis())
                .build();

        mWebSocket = mClient.newWebSocket(request, new WsListener());

    }

    class WsListener extends WebSocketListener {

        @Override

        public void onClosed(@NotNull WebSocket webSocket, int code, @NotNull String reason) {

            super.onClosed(webSocket, code, reason);
            Log.d(TAG, "onClosed: ");
        }

        @Override

        public void onClosing(@NotNull WebSocket webSocket, int code, @NotNull String reason) {

            super.onClosing(webSocket, code, reason);
            Log.d(TAG, "onClosing: " + code);
        }

        @Override

        public void onFailure(@NotNull WebSocket webSocket, @NotNull Throwable t, @Nullable Response response) {

            super.onFailure(webSocket, t, response);
            Log.d(TAG, "onFailure: " + t.getLocalizedMessage());
        }

        @Override

        public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) {

            super.onMessage(webSocket, text);

            Log.e(TAG, "客户端收到消息:" + text);

//            onWSDataChanged(DATE_NORMAL, text);

//测试发消息

//            webSocket.send("我是客户端，你好啊");

        }

        @Override

        public void onMessage(@NotNull WebSocket webSocket, @NotNull ByteString bytes) {

            super.onMessage(webSocket, bytes);
            Log.d(TAG, "onMessage: bytes=" + bytes);
        }

        @Override

        public void onOpen(@NotNull WebSocket webSocket, @NotNull Response response) {

            super.onOpen(webSocket, response);

            Log.d(TAG, "连接成功！");

        }

    }

//发送String消息

    public void send(final String message) {
        if (mWebSocket != null) {
            mWebSocket.send(message);
        }

    }

//发送byte消息

    public void send(final ByteString message) {
        if (mWebSocket != null) {
            mWebSocket.send(message);
        }

    }

//主动断开连接

    public void disconnect(int code, String reason) {
        if (mWebSocket != null) {
            mWebSocket.close(code, reason);
        }

    }
}
